package edu.visualization.homework.draw;

import java.awt.Color;
import java.awt.Graphics;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.visualization.homework.entity.Constructor;
import edu.visualization.homework.entity.TreeNode;
import edu.visualization.homework.entity.TreeUtils;

public class AnglePic extends JPanel {

	private static final long serialVersionUID = 1L;
	private TreeNode root;
	private int r;
	private int halfwidth;
	private int roundr;

	public AnglePic(TreeNode root, int width, int height, int r, int roundr) {
		super();
		this.setLayout(null);
		this.halfwidth = width / 2;
		this.root = root;
		this.r = r;
		this.roundr = roundr;
		this.setBounds(0, 0, width, height);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		paintRound(g, root);
		paintLine(g, root);
		// painR(g);
	}

	public void painR(Graphics g) {
		int depth = root.getDepth();
		g.setColor(Color.pink);
		for (int i = 0; i < depth; i++) {
			int length = roundr * i;
			g.drawOval(halfwidth - length, halfwidth - length, length * 2, length * 2);
		}
		g.setColor(Color.BLACK);
	}

	private void paintRound(Graphics g, TreeNode root) {
		if (root != null) {
			double a = Math.PI * root.getMidAngle() / 180;
			int y = (int) (-root.getP() * Math.sin(a)) + halfwidth;
			int x = (int) (root.getP() * Math.cos(a)) + halfwidth;
			String value = root.getValue();
			// value和p是正确的
			g.drawOval(x, y, r * 2, r * 2);
			g.setColor(Color.red);
			g.drawString(value, x + (int) (r * 0.85), y + (int) (r * 1.25));
			g.setColor(Color.black);
			if (root.getChildren() != null) {
				for (TreeNode node : root.getChildren()) {
					paintRound(g, node);
				}
			}
		}
	}

	private void paintLine(Graphics g, TreeNode root) {
		if (root != null) {
			if (root.getChildren() != null) {
				double a = Math.PI * root.getMidAngle() / 180;
				int yp = (int) (-root.getP() * Math.sin(a)) + halfwidth;
				int xp = (int) (root.getP() * Math.cos(a)) + halfwidth;
				for (TreeNode node : root.getChildren()) {
					a = Math.PI * node.getMidAngle() / 180;
					int y = (int) (-node.getP() * Math.sin(a)) + halfwidth;
					int x = (int) (node.getP() * Math.cos(a)) + halfwidth;
					g.setColor(Color.GRAY);
					g.drawLine(xp + r, yp + r, x + r, y + r);
					g.setColor(Color.BLACK);
					paintLine(g, node);
				}
			}
		}
	}

	public static void main(String[] args) {
		File file = new File("../homework");
		TreeNode root = Constructor.getTheTree(file);
		// 计算每一个node的深度
		TreeUtils.setTreeY(root, 0);
		// 获得树的高度和宽度
		int roundr = 40;
		// 计算每一个node的半周长度
		int depth = TreeUtils.getDepth(root);
		TreeUtils.setTreeP(root, roundr, depth);
		// 计算每一个node的开始角度和结束角度，使用的时候直接使用mid角度
		int numOfLeaves = TreeUtils.getNumOfLeaves(root);
		double stepAngle = (double) ((double) 360.0 / numOfLeaves);
		TreeUtils.getAngle(root, stepAngle, 0);
		// 计算整个界面的长度
		int width = roundr * depth;
		int higth = roundr * depth;
		System.out.println("width:" + width + " higth: " + higth);
		int r = 10;
		AnglePic jpanel = new AnglePic(root, width * 2, higth * 2, r, roundr);
		JFrame frame = new JFrame("径向布局: multiple tree");
		frame.setContentPane(jpanel);
		frame.setBounds(0, 0, jpanel.getWidth() + 50, jpanel.getHeight() + 50);
		frame.setVisible(true);
	}
}
